package com.yuntsg.subject_database.auxiliary;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.yuntsg.subject_database.entity.PublicEntity;
import com.yuntsg.subject_database.servic.SubjectDatabaseWord;
import com.yuntsg.subject_database.util.OftenUtils;
import com.yuntsg.subject_database.util.SmallTool;
import lombok.Data;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 原来data_auth_same_data表中，只保存一个result字段，而在核心论文合并表中没有这个字段，现把主要的几个核心内容给保存过去，以后不再读那个表了。
 * 贾新志 2022.02.25
 */
public class UniteTableAddDataInfo {
    // vm
//    public static void main(String[] args) throws SQLException, IOException {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long t0 = System.currentTimeMillis();
//        String uniteTable = "speciallist_23_02_03.result_core_paper_unite";
//        String resultTable = "scholar.data_auth_same_data";
//        SmallTool.printMemory();
//
//        String limit = "";
////        String limit = " limit 1";
//        Integer onePageNum = 1000;
//
//        Map<Integer, List<byte[]>> aIdAndDataInfoMap = new ConcurrentHashMap<>();
//
//        readTable(resultTable, onePageNum, limit, "id", "aid`,`result", "read", aIdAndDataInfoMap, true);
//        readTable(uniteTable, onePageNum, limit, "id", "article_id", "save", aIdAndDataInfoMap, true);
//
//        long t3 = System.currentTimeMillis();
//        System.out.println(format.format(new Date()) + " 程序全部结束，总用时：" + OftenUtils.Arith.div((t3 - t0), 60000) + " 分钟。");
//
//    }

//    public static void readTable(String tableName, Integer onePageNum, String limit, String autoId, String field, String type, Map<Integer, List<byte[]>> aIdAndDataInfoMap, boolean print) throws SQLException, IOException {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<Integer> tempNum = Collections.synchronizedList(new ArrayList<>());
//        int from = 1;
//        int max = 0;
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + tableName);
//        int count = tableNum.get(0).getInt("count");
//        if (count > 0) {
//            max = Db.use().queryNumber("select max(" + autoId + ") from " + tableName).intValue();
//        }
//        int page = max / onePageNum;
//        if (max % onePageNum != 0) {
//            page++;
//        }
//        List<OftenUtils.PageInfo> pageInfos = new ArrayList<>();
//        for (int i = 1; i <= page; i++) {
//            int start = (i - 1) * onePageNum + from;
//            int stop = i * onePageNum + from - 1;
//            if (stop > max) {
//                stop = max + onePageNum;
//            }
//            pageInfos.add(new OftenUtils.PageInfo(start, stop));
//        }
//        int numAll = pageInfos.size();
//        pageInfos.parallelStream().forEach(pg -> {
//            try {
//                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + tableName + " where " + autoId + " between " + pg.getFrom() + " and " + pg.getTo() + limit);
//                tempNum.add(pg.getFrom());
//                if (print && tempNum.size() % 1000 == 0) {
//                    System.out.println(format.format(new Date()) + " 现在开始： " + pg.getFrom() + " -- " + pg.getTo() + " 总条数： " + numAll + " 目前已处理： " + tempNum.size() + " 表名： " + tableName);
//                }
//                for (Entity entry : tableData) {
//                    if (null != entry) {
//                        if (type.equals("read")) {
//                            // 读取科室信息，这里的是aId，不是论文表中的自增主键。
//                            int aId = OftenUtils.getIntField(entry, "aid");
//                            if (aId > 0) {
//                                String result = OftenUtils.getStrField(entry, "result");
//                                List<byte[]> dataInfoList = new ArrayList<>();
//                                PublicEntity.AuthUnitInfoAndCode authUnitInfoAndCode = JSONObject.parseObject(result, PublicEntity.AuthUnitInfoAndCode.class);
//                                List<PublicEntity.authInfo> authList = authUnitInfoAndCode.getAuthUnitInfo().getAuth();
//                                for (PublicEntity.authInfo authInfo : authList) {
//                                    SubjectDatabaseWord.DataInfo dataInfo = new SubjectDatabaseWord.DataInfo();
//                                    dataInfo.setAuth(authInfo.getAuth());
//                                    dataInfo.setCnAu(authInfo.getCnAu());
//                                    Map<String, PublicEntity.detailedInfo> detailedInfo = authInfo.getDetailedInfo();
//                                    for (Map.Entry<String, PublicEntity.detailedInfo> detailedInfoMap : detailedInfo.entrySet()) {
//                                        PublicEntity.detailedInfo value = detailedInfoMap.getValue();
//                                        dataInfo.setStr(detailedInfoMap.getKey().toLowerCase().replaceAll(" +", " "));
//                                        dataInfo.setHosp(value.getHosp());
//                                        dataInfo.setUniv(value.getUniv());
//                                        dataInfo.setDept(value.getDept());
//                                        dataInfo.setColl(value.getColl());
//                                    }
//                                    byte[] encode = ProtobufProxy.create(SubjectDatabaseWord.DataInfo.class).encode(dataInfo);
//                                    dataInfoList.add(encode);
//                                }
//                                aIdAndDataInfoMap.put(aId, dataInfoList);
//                            }
//                        }
//                        if (type.equals("save")) {
//                            int aId = OftenUtils.getIntField(entry, "article_id");
//                            if (aIdAndDataInfoMap.containsKey(aId)) {
//                                List<byte[]> bytes = aIdAndDataInfoMap.get(aId);
//                                List<SubjectDatabaseWord.DataInfo> dataInfoList = new ArrayList<>();
//                                for (byte[] aByte : bytes) {
//                                    SubjectDatabaseWord.DataInfo dataInfo = ProtobufProxy.create(SubjectDatabaseWord.DataInfo.class).decode(aByte);
//                                    dataInfoList.add(dataInfo);
//                                }
//                                try {
//                                    Db.use().update(Entity.create().set("data_info", OftenUtils.emojiConvert(JSONUtil.toJsonStr(dataInfoList))), Entity.create(tableName).set("article_id", aId));
//                                } catch (SQLException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//                    }
//                }
//            } catch (SQLException | IOException e) {
//                e.printStackTrace();
//            }
//        });
//    }

//    @Data
//    public static class EnAuthAndCnAuth {
//        private String auth = "";
//        private String cnAu = "";
//    }


}
